package com.zrrd.servlet;

import com.alibaba.fastjson.JSON;
import com.zrrd.pojo.Tea;
import com.zrrd.service.TeaService;
import com.zrrd.service.impl.TeaServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author WanJl
 * @version 1.0
 * @title QueryTeaAll
 * @description
 * @create 2024/1/24 11:43
 */
@WebServlet("/tea/queryAllServlet")
public class QueryTeaAllServlet extends HttpServlet {
    TeaService teaService=new TeaServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //req.setCharacterEncoding(""); 把【请求】的字符集编码进行转换
        //调用service层对象的方法
        List<Tea> teas = teaService.queryTeaAll();
        //转换为JSON格式
        String jsonTeas = JSON.toJSONString(teas);

        //先为【响应】的数据设置字符集编码
        resp.setCharacterEncoding("UTF-8");
        //把查询到的数据响应给前端
        resp.getWriter().write(jsonTeas);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
